﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using RuneLog.GrandExchange;
using RuneLog.AdventerersLog;
using RuneLog.Stats;

namespace RuneLog
{
    partial class RuneLog
    {
        private IContainer components;
        private NotifyIcon notifyIcon;
        
        private ContextMenu trayMenu;
        private MenuItem trayMenuExit;
        private MenuItem trayMenuAdvlog;
        private MenuItem trayMenuGelookup;
        private MenuItem trayMenuStatLookup;

        private LookupForm lookup;
        private AdvlogLookupForm advLookup;
        private StatsLookupForm statsLookup;
        private static String trayIconFile
        {
            get
            {
                return "resources/chalkboard.ico";
            }
        }        

        private void InitializeComponent()
        {
            this.components = new Container();

            this.notifyIcon = new NotifyIcon(this.components);
            this.notifyIcon.Icon = new Icon(trayIconFile);

            trayMenu = new ContextMenu();
            trayMenuAdvlog = new MenuItem("Adventurer's Log"); trayMenu.MenuItems.Add(trayMenuAdvlog);
            trayMenuGelookup = new MenuItem("GE Lookup"); trayMenu.MenuItems.Add(trayMenuGelookup);
            trayMenuStatLookup = new MenuItem("Stat Lookup"); trayMenu.MenuItems.Add(trayMenuStatLookup);
            trayMenuExit = new MenuItem("Exit"); trayMenu.MenuItems.Add(trayMenuExit);

            this.notifyIcon.ContextMenu = trayMenu;

            trayMenuStatLookup.Click += (sender, evt) =>
            {
                if (statsLookup == null || statsLookup.IsDisposed)
                {
                    statsLookup = new StatsLookupForm();
                    statsLookup.Show();
                }
                
            };

            trayMenuExit.Click += (sender, evt) => 
            {
                ExitApplication(sender);
            };

            trayMenuAdvlog.Click += (sender, evt) => 
            {
                if (advLookup == null || advLookup.IsDisposed)
                {
                    advLookup = new AdvlogLookupForm();
                    advLookup.Show();
                }
                if (advLookup.WindowState == FormWindowState.Minimized)
                    advLookup.WindowState = FormWindowState.Normal;
                else advLookup.Focus();
            };

            trayMenuGelookup.Click += (sender, evt) =>
            {
                if (lookup == null || lookup.IsDisposed)
                {
                    lookup = new LookupForm();
                    lookup.Show();
                }
                if (lookup.WindowState == FormWindowState.Minimized)
                    lookup.WindowState = FormWindowState.Normal;
                else lookup.Focus();
            };

            this.notifyIcon.Visible = true;
        }

        private void ExitApplication(Object sender)
        {
            notifyIcon.Icon = null;
            foreach ( IComponent c in this.components.Components )
            {
                c.Dispose();
            }
            Application.Exit();
        }
    }
}
